﻿using System.Collections.Generic;
using System;

namespace HREngine.Bots
{
    public partial class Behavior动物园 : Behavior
    {
        public override string BehaviorName() { return "动物园"; }
        PenalityManager penman = PenalityManager.Instance;
        // 核心，场面值
        public override float getPlayfieldValue(Playfield p)
        {
            if (p.value > -200000) return p.value;
            float retval = 0;

            // 出牌序列数量
            int count = p.playactions.Count;
            int ownActCount = 0;

            bool foundKnife = false;
            foreach (Minion mm in p.ownMinions)
            {
                if (mm.handcard.card.chnName == "飞刀杂耍者")
                {
                    foundKnife = true;
                }
            }

            // 排序问题！！！！
            for (int i = 0; i < count; i++)
            {
                Action a = p.playactions[i];
                ownActCount++;
                switch (a.actionType)
                {
                    // 英雄攻击
                    case actionEnum.attackWithHero:
                        continue;
                    case actionEnum.useHeroPower:
                    case actionEnum.playcard:
                        break;
                    default:
                        continue;
                }
                if (foundKnife && a.actionType == actionEnum.playcard && a.card.card.type == CardDB.cardtype.MOB)
                {
                    retval += 5 - i;
                }
                switch (a.card.card.chnName)
                {
                    // 排序优先
                    case "死亡缠绕":
                        if(!foundKnife)
                        retval -= i * 7 - 7;
                        break;
                    case "生命分流":
                        retval -= i * 3;
                        break;
                    case "恐狼前锋":
                        retval -= i;
                        break;
                    case "力量的代价":
                    case "飞刀杂耍者":
                        retval -= i * 2;
                        break;
                    case "末日守卫":
                    case "灵魂之火":
                    case "阿古斯防御者":
                        retval += i;
                        break;
                }
            }

            retval += getGeneralVal(p);
            retval += p.owncards.Count * 2;
            // 危险血线
            int hpboarder = 7;
            // 抢脸血线
            int aggroboarder = 24;
            retval += getHpValue(p, hpboarder, aggroboarder);
            
            retval += getSecretPenality(p); // 奥秘的影响
            p.value = retval;
            return retval;
        }


        // 敌方随从价值 主要等于 
        public override int getEnemyMinionValue(Minion m, Playfield p)
        {
            int retval = 5;
            if (m.Angr > 0)
                retval += m.Hp * 2; 
            if (!m.frozen && !(m.cantAttack && m.name != CardDB.cardName.argentwatchman))
            {
                // 保守
                if (p.ownHero.Hp <= 8)
                {
                    retval += m.Angr * 20 + 10;
                }
                retval += m.Angr * 2;
                if (m.windfury) retval += m.Angr * 2;
            }
            if (m.silenced) return retval;

            if (!m.handcard.card.isSpecialMinion)
            {
                if (m.Angr == 0) retval -= 7;
                else if (m.Angr <= 2 && m.Hp <= 2 && !m.divineshild) retval -= 5;
            }
            else retval += m.handcard.card.rarity;

            if (m.taunt) retval += 5;
            if (m.divineshild) retval += m.Angr;
            if (m.divineshild && m.taunt) retval += 5;
            if (m.stealth) retval += 1;

            if (m.poisonous)
            {
                retval += 4;
                if (p.ownMinions.Count < p.enemyMinions.Count) retval += 10;
            }
            if (m.lifesteal) retval += m.Angr;

            if (m.handcard.card.targetPriority >= 1 && !m.silenced)
            {
                retval += m.handcard.card.targetPriority;
            }
            if (m.name == CardDB.cardName.nerubianegg && m.Angr <= 3 && !m.taunt) retval = 0;
            retval += m.synergy;
            switch (m.handcard.card.chnName){
                // 解不掉游戏结束
                case "加基森拍卖师":
                case "任务达人":
                case "苔原犀牛":
                case "飞刀杂耍者":
                case "迦顿男爵":
                case "法力之潮图腾":
                case "巫师学徒":
                case "火舌图腾":
                    retval += 100;
                    break;
                // 解不掉大劣势
                case "大法师安东尼达斯":
                    retval += 30;
                    break;
                // 我劝你最好解了
                case "北郡牧师":
                case "狂野炎术士":
                case "奥金尼灵魂祭司":
                case "暴风城勇士":
                case "法力浮龙":
                case "雷欧克":
                case "森林狼":
                case "饥饿的秃鹫":
                case "恐狼前锋":
                case "食腐土狼":
                case "先知维纶":
                case "炎魔之王拉格纳罗斯":
                case "铸甲师":
                    retval += 20;
                    break;
                case "末日预言者":
                    foreach(Minion mm in p.ownMinions)
                    {
                        retval += (mm.Hp + mm.Angr) * 10;
                    }
                    break;
                // 带点异能随手解一下吧
                case "年轻的女祭司":
                    retval += 3;
                    break;
                // 大哥，求别解
                case "希尔瓦娜斯":
                    retval -= 10;
                    break;                
            }
            return retval;
        }

        public override int getMyMinionValue(Minion m, Playfield p)
        {
            int retval = 2;
            // 已死随从
            if (m.destroyOnOwnTurnEnd) return 0;

            retval += m.Hp * 2;
            if (!m.cantAttack || !m.Ready || !m.frozen)
            {
                retval += m.Angr;
            }
            else
            {
                retval += m.Angr / 4;
            }
            // 鱼人种族价值
            //if (m.handcard.card.race == 14)
            //{
            //    retval += m.Hp + m.Angr + 2;
            //}
            // 风怒价值
            if ((!m.playedThisTurn || m.rush == 1 || m.charge == 1) && m.windfury) retval += m.Angr;
            // 圣盾价值
            if (m.divineshild ) retval += m.Angr / 2 + 1;
            // 潜行价值
            if (m.stealth) retval += m.Angr / 3 + 1;
            // 吸血
            if (m.lifesteal) retval += m.Angr / 3 + 1;
            // 圣盾嘲讽
            if (m.divineshild && m.taunt) retval += 4;
            // retval += m.synergy;
            // retval += getSpeicalCardVal(m.handcard.card);
            return retval;
        }

        public override int getSirFinleyPriority(List<Handmanager.Handcard> discoverCards)
        {
            
            return -1; //comment out or remove this to set manual priority
            int sirFinleyChoice = -1;
            int tmp = int.MinValue;
            for (int i = 0; i < discoverCards.Count; i++)
            {
                CardDB.cardName name = discoverCards[i].card.name;
                if (SirFinleyPriorityList.ContainsKey(name) && SirFinleyPriorityList[name] > tmp)
                {
                    tmp = SirFinleyPriorityList[name];
                    sirFinleyChoice = i;
                }
            }
            return sirFinleyChoice;
        }
        public override int getSirFinleyPriority(CardDB.Card card)
        {
            return SirFinleyPriorityList[card.name];
        }

        private Dictionary<CardDB.cardName, int> SirFinleyPriorityList = new Dictionary<CardDB.cardName, int>
        {
            //{HeroPowerName, Priority}, where 0-9 = manual priority
            { CardDB.cardName.lesserheal, 0 }, 
            { CardDB.cardName.shapeshift, 6 },
            { CardDB.cardName.fireblast, 7 },
            { CardDB.cardName.totemiccall, 1 },
            { CardDB.cardName.lifetap, 9 },
            { CardDB.cardName.daggermastery, 5 },
            { CardDB.cardName.reinforce, 4 },
            { CardDB.cardName.armorup, 2 },
            { CardDB.cardName.steadyshot, 8 }
        };

        public override int getComboPenality(CardDB.Card card, Minion target, Playfield p)
        {
            // 无法选中
            if (target != null && target.untouchable)
            {
                return 100000;
            }

            // 初始惩罚值
            int pen = 0;
            pen += penman.getSilencePenality(card.name, target, p);
            int cost1 = 0;
            int cost2 = 0;
            if (p.ownMaxMana == 1)
                foreach (Handmanager.Handcard hc in p.owncards)
                {
                    if (hc.manacost == 1 && hc.card.type == CardDB.cardtype.MOB) cost1++;
                    else if (hc.manacost == 2) cost2++;
                }
            foreach(Minion m in p.enemyMinions)
            {
                if(m.handcard.card.cardIDenum == CardDB.cardIDEnum.VAN_NEW1_021 && card.type == CardDB.cardtype.MOB 
                    && !card.Charge && card.chnName != "叫嚣的中士" && card.chnName != "恐狼前锋")
                {
                    return 1000;
                }
            }
            switch (card.chnName)
            {
                case "持盾卫士":
                case "银色侍从":
                    pen -= 3;
                    if (p.ownMaxMana == 1) pen -= 3;
                    break;
                case "力量的代价":
                    if(target!=null)
                    {
                        pen += target.Hp + target.Angr + 8;
                    }
                    break;
                case "幸运币":
                    pen += 1;
                    break;
                case "麻风侏儒":
                    pen -= 4;
                    break;
                //-----------------------------超模补正-----------------------------------
                case "烈焰小鬼":
                    return -5;
                // 亡语补正
                case "麦田傀儡":
                    pen -= 5;
                    break;
                case "死亡缠绕":
                    if (target.Hp > 1 + p.spellpower)
                        pen += 5;
                    else if(!target.divineshild)
                        pen -= 50;
                    break;                        
                //-----------------------------武器-----------------------------------
                
                //-----------------------------针对卡---------------------------------
                //-----------------------------配合-----------------------------------
                case "年轻的女祭司":
                    if( p.ownMinions.Count > 0 ) pen -= 20;
                    if( p.ownMinions.Count < 1 && p.ownMaxMana < 2) pen += 2;
                    break;
                case "血色十字军战士":
                    pen -= 10;
                    break;
                case "破碎残阳祭司":
                    if (target != null) pen -= 5;
                    break;
                case "飞刀杂耍者":   
                    if( p.ownMaxMana < 3 && p.enemyMinions.Count == 0 ) pen -= 50;
                    // 白送
                    else if(p.ownMinions.Count == 0 && p.enemyMinions.Count > 0 && p.mana == 2) pen += 10;
                    break;
                case "末日守卫":
                    if (p.owncards.Count == 0) pen -= 10;
                    if (p.owncards.Count <= 1) pen -= 50;
                    else if(p.owncards.Count == 2) pen -= 10;
                    else pen += 5;
                    pen += p.owncards.Count * 2;
                    break; 
                case "灵魂之火":
                    if (p.owncards.Count == 0) pen -= 10;
                    if ( p.owncards.Count <= 1 ) pen -= 50;
                    else pen += 10;
                    if (target.isHero) pen += 4;
                    // 双魂火互卡
                    pen += p.owncards.Count * 2;
                    int soulCnt = 0;
                    foreach(Handmanager.Handcard hc in p.owncards)
                    {
                        if(hc.card.chnName == "灵魂之火")
                        {
                            soulCnt++;
                        }
                    }
                    if(soulCnt > 1)
                    {
                        pen -= 6;
                    }
                    break; 
                //-----------------------------buff-----------------------------------
                case "阿古斯防御者":
                    if( p.ownMinions.Count > 1) pen -= 5;
                    if (p.ownMinions.Count > 2) pen -= 2;
                    break;
                case "叫嚣的中士":
                    if (target == null || !target.Ready || !target.own) pen += 5;
                    break;
                case "黑铁矮人":
                    if (target == null || !target.Ready || !target.own) pen += 5;
                    break;

                //-----------------------------英雄技能-----------------------------------
                case "生命分流":
                    pen += 8;
                    if ( p.enemyHero.Hp <= 5 && p.mana >= 7 ) pen -= 50;
                    break;
            }
            return pen;
        }

        public override int getHpValue(Playfield p, int hpboarder, int aggroboarder)
        {
            int retval = 0;
            // 血线安全
            if (p.ownHero.Hp + p.ownHero.armor > hpboarder)
            {
                retval += 2;
            }
            // 快死了
            else
            {
                if (p.nextTurnWin()) retval -= (hpboarder + 1 - p.ownHero.Hp - p.ownHero.armor);
                else retval -= 2 * (hpboarder + 1 - p.ownHero.Hp - p.ownHero.armor) * (hpboarder + 1 - p.ownHero.Hp - p.ownHero.armor);
            }
            if (p.enemyHero.Hp + p.enemyHero.armor < 7)
            {
                retval += (10 - p.enemyHero.Hp - p.enemyHero.armor) * 20;
            }
            // 对手血线安全
            if (p.enemyHero.Hp + p.enemyHero.armor > aggroboarder)
            {
                retval += 4 - p.enemyHero.Hp - p.enemyHero.armor + aggroboarder;
            }
            // 开始打脸
            else if (p.enemyHero.Hp + p.enemyHero.armor <= aggroboarder && p.enemyHero.Hp + p.enemyHero.armor > aggroboarder / 3)
            {
                retval += 4 * (aggroboarder + 1 - p.enemyHero.Hp - p.enemyHero.armor);
            }
            else
            {
                retval += 6 * (aggroboarder + 1 - p.enemyHero.Hp - p.enemyHero.armor);
            }
            return retval;
        }
    }
}